<? 
	$title="Delete category"; 
	$pageid="5";
	$accessid="a00";

	require("required/dbconnect.php"); 
	require("required/function.php");
	
	
	
	
	if(isset($_POST['btnsubmit']))
	{
		$id=$_POST['id'];
		$get_info=mysql_query("select * from os_sub_category where id=$id");
		$m=mysql_fetch_array($get_info,MYSQL_ASSOC);
				
		if($m['pic']!="")
		{
			$product_pic="../subcat/".$m['pic'].".gif";
			unlink($product_pic);
			
		}
		
		$get_product=mysql_query("select * from os_products where sub_cat_id=$id");
		
		if(mysql_num_rows($get_product)>0)
		{
			while($p=mysql_fetch_array($get_product))
			{
				if($p['pic']!="")
				{
					$product_pic="../products/".$p['pic'].".gif";
					$product_pic_s="../products/thumbnails/".$p['pic'].".gif";
					unlink($product_pic);
					unlink($product_pic_s);
				}
				
				$get_photo=mysql_query("select id,name from os_productpic where product_id=".$p['id']);
		
				if(mysql_num_rows($get_photo)>0)
				{
					while($b=mysql_fetch_array($get_photo,MYSQL_ASSOC))
					{
						$photo_name=$b['name'];
						$file_l = "../productpic/$photo_name";
						$file_s = "../productpic/thumbnails/$photo_name";
						unlink($file_l);
						unlink($file_s);
						$delete=mysql_query("delete from os_productpic where id=".$b['id']);
					}
				}
				
				$delete_product=mysql_query("delete from os_products where id=".$p['id']);
			}
		}
		
		$delete=mysql_query("delete from os_sub_category where id=$id")or die(mysql_error());
		header("Location:subcat_management.php?action=2");
	}
	else
	{
	
		$id=$_GET['id'];
		require("required/header.php");
	
?>
<table width="95%" cellpadding="2" cellspacing="1">
	
	<td valign="top" align="center">
	<table width="100%">
	<tr>
		<td height="20" colspan="7" align="left" class="title">Delete record</td>
	</tr>
	<tr>
		<td>This will delete all products under this category, confirm to delete?</td>
	</tr>
	<tr>
		<td colspan="3" align="center">
		<form action="<?= $_SERVER['PHP_SELF'] ?>" method="post">
		<input type="hidden" value="<?= $id ?>" name="id">
		<input type="submit" value="Yes" name='btnsubmit' class="button">
		<input type="button" value="No" onClick="history.go(-1)" class="button">
		</form>
		</td>
	</tr>
	</table>
	</td>
 
</table>
<? } require("required/footer.php"); ?>
